package org.ykhl.sdk.appserver;

import org.ykhl.mq.commons.Constants;
import org.ykhl.mq.commons.Utils;
import org.ykhl.mq.commons.receiver.MessageListener;
import org.ykhl.mq.commons.receiver.MessageReceiver;
import org.ykhl.mq.commons.receiver.MessageReceiverKafkaImpl;

/**
 * 用于创建{@link MessageReceiver}对象的静态工厂类。
 * 
 * @author Zhang Kefei
 *
 */
public class MessageReceiverFactory {
	private MessageReceiverFactory(){}
	private static final String DEFAULT_NAME_SERVER_ADDRESS = Constants.KAFKA_SERVER_LIST_DEV;

	public static MessageReceiver newInstance() {
		String authKey = Utils.getAuthIdFromKeyStoreUnchecked(Constants.KAFKA_SECURITY_KEYSTORE_LOCATION);

		return newInstance(authKey);
	}

	/**
	 * 创建一个MessageReceiver实例
	 * 
	 * @param authKey 申请获得的密钥
	 * @return
	 */
	public static MessageReceiver newInstance(String authKey){
		return newInstance(DEFAULT_NAME_SERVER_ADDRESS, authKey);
	}
	
	/**
	 * 创建一个MessageReceiver实例
	 * 
	 * @param authKey 申请获得的密钥
	 * @param listener 消息监听器
	 * @return
	 */
	public static MessageReceiver newInstance(String authKey, MessageListener listener){
		return newInstance(DEFAULT_NAME_SERVER_ADDRESS, authKey, listener);
	}

	/**
	 * 创建一个MessageReceiver实例
	 *
	 * @param serverAddress 连接的服务器地址
	 * @param authKey 申请获得的密钥
	 * @return
	 */
	public static MessageReceiver newInstance(String serverAddress, String authKey){
		return new MessageReceiverKafkaImpl(serverAddress, authKey,
				null, Constants.Role.APPSERVER);
	}

	/**
	 * 创建一个MessageReceiver实例
	 *
	 * @param serverAddress 连接的服务器地址
	 * @param authKey 申请获得的密钥
	 * @param listener 消息监听器
	 * @return
	 */
	public static MessageReceiver newInstance(String serverAddress, String authKey,
											  MessageListener listener){
		return new MessageReceiverKafkaImpl(serverAddress, authKey,
				null, Constants.Role.APPSERVER, listener);
	}

	/**
	 * 创建一个MessageReceiver实例
	 *
	 * @param securityFilePath 指定证书文件的路径
	 * @return
	 */
	public static MessageReceiver newInstanceWithSecurityFilePath(String securityFilePath) {
		return newInstanceWithSecurityFilePath(DEFAULT_NAME_SERVER_ADDRESS, securityFilePath);
	}

	/**
	 * 创建一个MessageReceiver实例
	 *
	 * @param serverAddress 连接的服务器地址
	 * @param securityFilePath 指定证书文件的路径
	 * @return
	 */
	public static MessageReceiver newInstanceWithSecurityFilePath(
			String serverAddress, String securityFilePath) {
		return new MessageReceiverKafkaImpl(serverAddress, null, securityFilePath, Constants.Role.APPSERVER);
	}

	/**
	 * 创建一个MessageReceiver实例
	 *
	 * @param securityFilePath 指定证书文件的路径
	 * @param listener 消息监听器
	 * @return
	 */
	public static MessageReceiver newInstanceWithSecurityFilePath(
			String securityFilePath, MessageListener listener) {

		return newInstanceWithSecurityFilePath(DEFAULT_NAME_SERVER_ADDRESS,
				securityFilePath, listener);
	}

	/**
	 * 创建一个MessageReceiver实例
	 *
	 * @param serverAddress 连接的服务器地址
	 * @param securityFilePath 指定证书文件的路径
	 * @param listener 消息监听器
	 * @return
	 */
	public static MessageReceiver newInstanceWithSecurityFilePath(
			String serverAddress, String securityFilePath, MessageListener listener) {

		return new MessageReceiverKafkaImpl(serverAddress,
				null, securityFilePath, Constants.Role.APPSERVER, listener);
	}
}
